package com.java.wechat.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

/**
 * @author jiangli
 * @date 2019/10/29 21:52
 */
@Component
public class WSServer {

	//单例
	private static class SingletonWSServer {
		static final WSServer INSTANCE = new WSServer();
	}

	public static WSServer getInstance() {
		return SingletonWSServer.INSTANCE;
	}

	private EventLoopGroup masterGroup;
	private EventLoopGroup workerGroup;
	private ServerBootstrap serverBootstrap;
	private ChannelFuture future;

	public WSServer() {
		masterGroup = new NioEventLoopGroup();
		workerGroup = new NioEventLoopGroup();
		serverBootstrap = new ServerBootstrap();
		serverBootstrap.group(masterGroup,workerGroup)
				.channel(NioServerSocketChannel.class)
				.childHandler(new WSServerInitializer());
	}

	public void start() {
		this.future = serverBootstrap.bind(8088);
		System.out.println("netty websocket server 启动完毕...");
	}

}
